<template>
  <div>
    <div style="text-align: right; margin: 10px">
      <el-button size="small" type="danger" @click="handleBatchDel" plain
        >批量删除</el-button
      >
    </div>

    <el-table
      :data="list"
      border
      style="width: 100%; margin-left: 10px; margin-right: 10px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column prop="integralStart" label="开始积分"> </el-table-column>
      <el-table-column prop="integralEnd" label="截止积分"> </el-table-column>
      <el-table-column prop="borrowAmount" label="借款额度"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import integralGradeApi from "@/api/integralGrade";
export default {
  data() {
    return {
      list: [],
      checkedRows: [],
    };
  },
  //页面渲染前执行
  created() {
    this.initData();
  },
  //页面渲染后执行
  mounted() {},
  methods: {
    //
    handleSelectionChange(selection) {
      // console.log(selection);
      this.checkedRows = selection;
    },
    //批量删除按钮事件函数
    handleBatchDel() {
      //获取选中的批量删除的行的id
      // console.log("hehe", this.checkedRows);
      // 1、判断选中删除的集合是否有数据
      if (!this.checkedRows || this.checkedRows.length === 0) {
        //没有选中要删除的行
        this.$message.warning("请选择要删除的记录");
        return;
      }
      //2、获取选中数据的id集合:
      let ids = [];
      this.checkedRows.forEach((row) => {
        ids.push(row.id);
      });
      //3、提交批量删除的请求
      integralGradeApi.batchDel(ids).then((r) => {
        this.$message.success("删除成功");
        this.initData();
      });
    },
    //操作按钮组事件函数
    handleEdit(idx, row) {
      console.log(idx, row);
      //跳转到编辑页面
      // this.$router.push("/integral-grade/edit?id=" + row.id);
      this.$router.push("/integral-grade/edit/" + row.id);
    },
    handleDelete(idx, row) {
      // console.log(idx, row);
      this.$confirm(
        `此操作将永久删除【${row.borrowAmount}】积分等级, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          //提交删除数据的异步请求，成功后弹出提示
          integralGradeApi.deleteById(row.id).then((r) => {
            //成功提示
            this.$message.success("删除成功");
            //刷新页面数据
            this.initData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    initData() {
      integralGradeApi.list().then((r) => {
        // console.log(r);
        this.list = r.data.items;
        //r代表响应报文响应体中的数据：统一响应类R的对象
      });
      //utils/request.js中给axios配置了响应拦截器 处理了失败的结果
    },
  },
};
</script>
